package com.weirn.springmvc.action;

import com.weirn.springmvc.entity.Animal;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yp-tc-2563 on 2017/9/27.
 */
@Controller
public class ViewController {
    @RequestMapping("/view")
    public ModelAndView view(HttpServletRequest request){
        String path = request.getParameter("path") + "";
        ModelAndView mav = new ModelAndView();
        mav.setViewName("ftl/" + path);
        return mav;
    }

    @RequestMapping("/freemarker")
    public String getFirstPage(Model model) {
        model.addAttribute("test", "my name is freemarker");
        return "ftl/hello";
    }

    //
    @RequestMapping("/base")
    public String getBase(Model model) {

        //map
        model.addAttribute("test", "my name is freemarker");
        Map<String,String> latestProduct = new HashMap<String,String>();
        latestProduct.put ( "url","freemarker" );
        latestProduct.put ( "name","hello" );

        model.addAllAttributes ( latestProduct );
        model.addAttribute("latestProduct", latestProduct);
        model.addAttribute ( "user" ,"Big Joe");

        Map<String,Object> mouse = new HashMap<> (  );
        mouse.put ( "name","mouse" );
        mouse.put ( "price",10 );
        Map<String,Object> elephant = new HashMap<> (  );
        elephant.put ( "name","elephant" );
        elephant.put ( "price",20 );
        Map<String,Object> python = new HashMap<> (  );
        python.put ( "name","python" );
        python.put ( "price",30 );

        // list
        List<Map> animals = new ArrayList<> (  );
        animals.add ( mouse );
        animals.add ( elephant );
        animals.add ( python );

        model.addAttribute ( "animals" ,animals);

        List<String> fruits = new ArrayList<> (  );
        fruits.add ( "apple" );
        fruits.add ( "banana" );
        fruits.add ( "age" );

        model.addAttribute ( "fruits" ,fruits);

        return "ftl/base";
    }
}
